package run.bottle.app.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

/**
 * json工具类
 *
 * @author: Lycheng
 * @date: 2020/9/29 21:05
 */
public class JsonUtil {

    public static Map jsonToMap(String obj) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(obj, Map.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
            e.printStackTrace();
        }
        return null;
    }

    public static Object jsonToBean(String obj, Class BClass) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(obj, BClass);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
            e.printStackTrace();
        }
        return null;
    }

    public static String objectToJson(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
            e.printStackTrace();
        }
        return null;
    }

}
